//
//  ICMyRedPacketHeaderView.m
//  ICome
//
//  Created by 席鹏飞 on 2016/11/9.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICMyRedPacketHeaderView.h"
#import "ICHeadImageView.h"
#import "UIImage+AvatarGeneration.h"
#import "ICHeaderFile.h"

#define ImageHeight 45
#define LineWidth 3

@interface ICMyRedPacketHeaderView()
@property (nonatomic, strong) ICHeadImageView *headerImageView;  // 头像
@property (nonatomic, strong) UILabel *nameLabel; // 姓名
@property (nonatomic, strong) UILabel *totalIncomeLabel; // 总收入:
@property (nonatomic, strong) UILabel *totalExpenditureLabel; // 总支出:
@property (nonatomic, strong) UILabel *incomeValueLabel; // 总收入值
@property (nonatomic, strong) UILabel *expenditureValueLabel; // 总支出值
@end

@implementation ICMyRedPacketHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.stretchImageView];
        [self addSubview:self.arcImageView];
        [self addSubview:self.headerImageView];
        [self addSubview:self.nameLabel];
        [self addSubview:self.yearButton];
        [self addSubview:self.incomeValueLabel];
        [self addSubview:self.expenditureValueLabel];
        [self addSubview:self.totalIncomeLabel];
        [self addSubview:self.totalExpenditureLabel];
        [self drawCirque];

//        [self.headerImageView.imageView sd_setImageWithURL:[NSURL URLWithString:OGLIMAGEURL([ICUser currentUser].photoId)] placeholderImage:[UIImage imageNamed:@"App_personal_headimg"]];
        NSString *URL = [ICTools getMinImageURL:([ICUser currentUser].photoId)];
        [UIImage addAvatariamgeView:self.headerImageView.imageView withUrlStr:URL witheId:[ICUser currentUser].eId withName:[ICUser currentUser].eName];
        self.nameLabel.text = [ICUser currentUser].eName;
    }
    return self;
}

- (void)drawCirque {
    CAShapeLayer *arcLayer = [CAShapeLayer layer];
    arcLayer.fillColor = [[UIColor clearColor] CGColor];
    arcLayer.strokeColor = ICRGB(0xFFF5F5).CGColor;
    arcLayer.lineCap = kCALineCapButt;//指定线的边缘是圆的
    arcLayer.lineWidth = LineWidth;//线的宽度
    arcLayer.opacity = 0.25;
    [self.layer addSublayer:arcLayer];
    
    CGPoint center = CGPointMake(self.width * 0.5, LineWidth + 2 + ImageHeight * 0.5);
    CGFloat radius = center.y - LineWidth * 0.5 - 2;
    UIBezierPath *arcPath = [UIBezierPath bezierPathWithArcCenter:center radius:radius  startAngle:0 endAngle:M_PI * 2 clockwise:YES];//构建圆形
    arcLayer.path = arcPath.CGPath;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.stretchImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
        make.height.mas_equalTo(1);
    }];
    [self.arcImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.right.equalTo(self.mas_right);
    }];
    [self.headerImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.mas_top).offset(LineWidth+2);
        make.width.mas_equalTo(ImageHeight);
        make.height.mas_equalTo(ImageHeight);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.headerImageView.mas_bottom).offset(7);
        make.height.mas_equalTo(20);
    }];
    [self.yearButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self.nameLabel.mas_bottom);
        make.bottom.equalTo(self.mas_bottom);
    }];
    [self.totalIncomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLabel.mas_centerY);
        make.left.equalTo(self.mas_left).offset(25);
        make.height.equalTo(self.nameLabel.mas_height);
    }];
    [self.totalExpenditureLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.nameLabel.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-25);
        make.height.equalTo(self.nameLabel.mas_height);
    }];
    [self.incomeValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.yearButton.mas_centerY);
        make.left.equalTo(self.mas_left).offset(25);
        make.height.equalTo(self.yearButton.mas_height);
        make.right.equalTo(self.yearButton.mas_left).offset(3);
    }];
    [self.expenditureValueLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.yearButton.mas_centerY);
        make.right.equalTo(self.mas_right).offset(-25);
        make.height.equalTo(self.yearButton.mas_height);
        make.left.equalTo(self.yearButton.mas_right).offset(3);
    }];
}

- (void)setModel:(ICMyHistoryListModel *)model {
    self.incomeValueLabel.text = model.receiveSum;
    self.expenditureValueLabel.text = model.sendSum;
}

#pragma mark - event response

- (void)yearBtnClicked {
    if ([self.delegate respondsToSelector:@selector(dateBtnClicked)]) {
        [self.delegate dateBtnClicked];
    }
}

- (NSString *)getYear {
    NSDate *date = [NSDate date];
    return [NSString stringWithFormat:@"%ld年", date.year];
}

#pragma mark - Getter

- (UIImageView *)stretchImageView {
    if (!_stretchImageView) {
        _stretchImageView = [[UIImageView alloc] init];
        _stretchImageView.image = [UIImage imageWithColor:ICRGB(0xe44e4e)];
    }
    return _stretchImageView;
}

- (UIImageView *)arcImageView {
    if (!_arcImageView) {
        _arcImageView = [[UIImageView alloc] init];
        _arcImageView.image = [UIImage imageNamedFromiComeSDKBundle:@"icon_my_redpacket_arc"];
    }
    return _arcImageView;
}

- (ICHeadImageView *)headerImageView {
    if (!_headerImageView) {
        _headerImageView = [[ICHeadImageView alloc] init];
    }
    return _headerImageView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.textColor = ICRGB(0x333333);
        _nameLabel.font = ICFont(15.0 * Level);
    }
    return _nameLabel;
}

- (UIButton *)yearButton {
    if (!_yearButton) {
        CGFloat level = [self getNewLevel:Level];
        _yearButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_yearButton setTitle:[self getYear] forState:UIControlStateNormal];
        [_yearButton setTitleColor:ICRGB(0xF6A623) forState:UIControlStateNormal];
        [_yearButton setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_my_redpacket_year"] forState:UIControlStateNormal];
        [_yearButton setImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_my_redpacket_year"] forState:UIControlStateHighlighted];
        _yearButton.titleLabel.font = ICFont(14.0 * Level);
        _yearButton.titleEdgeInsets = UIEdgeInsetsMake(0, -15 * level, 0, 15 * level);
        _yearButton.imageEdgeInsets = UIEdgeInsetsMake(0, 50 * level, 0, -50 * level);
        [_yearButton addTarget:self action:@selector(yearBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _yearButton;
}

- (UILabel *)incomeValueLabel {
    if (!_incomeValueLabel) {
        _incomeValueLabel = [[UILabel alloc] init];
        _incomeValueLabel.textColor = ICRGB(0x89c44d);
        _incomeValueLabel.font = ICFont(30.0 * Level);
    }
    return _incomeValueLabel;
}

- (UILabel *)expenditureValueLabel {
    if (!_expenditureValueLabel) {
        _expenditureValueLabel = [[UILabel alloc] init];
        _expenditureValueLabel.textColor = ICRGB(0xe44e4e);
        _expenditureValueLabel.textAlignment = NSTextAlignmentRight;
        _expenditureValueLabel.font = ICFont(30.0 * Level);
    }
    return _expenditureValueLabel;
}

- (UILabel *)totalIncomeLabel {
    if (!_totalIncomeLabel) {
        _totalIncomeLabel = [[UILabel alloc] init];
        _totalIncomeLabel.text = @"总收入:";
        _totalIncomeLabel.textColor = ICRGB(0x999999);
        _totalIncomeLabel.font = ICFont(15.0 * Level);
    }
    return _totalIncomeLabel;
}

- (UILabel *)totalExpenditureLabel {
    if (!_totalExpenditureLabel) {
        _totalExpenditureLabel = [[UILabel alloc] init];
        _totalExpenditureLabel.text = @"总支出:";
        _totalExpenditureLabel.textColor = ICRGB(0x999999);
        _totalExpenditureLabel.textAlignment = NSTextAlignmentRight;
        _totalExpenditureLabel.font = ICFont(15.0 * Level);
    }
    return _totalExpenditureLabel;
}

- (CGFloat)getNewLevel:(CGFloat)level {
    NSString *le = [NSString stringWithFormat:@"%.1f", level];
    if ([le isEqualToString:@"1.0"]) {
        return level -= 0.05;
    }else if ([le isEqualToString:@"1.1"]) {
        return level -= 0.08;
    }else if ([le isEqualToString:@"1.2"]) {
        return level -= 0.11;
    }else if ([le isEqualToString:@"1.3"]) {
        return level -= 0.14;
    }
    return level;
}

@end
